package com.xinkao.example.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xinkao.example.domain.entity.UploadFileEntity;
import com.xinkao.example.dao.UploadFileDao;
import com.xinkao.example.service.UploadFileService;
import com.xinkao.example.util.CommonUtil;
import javax.annotation.Resource;
import org.springframework.transaction.annotation.Transactional;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * 文件上传
 * @Author
 * @Date 2021-08-27 10:31:50
 */
@Service("uploadFileService")
public class UploadFileServiceImpl extends ServiceImpl<UploadFileDao, UploadFileEntity>
        implements UploadFileService {

    @Resource
    private UploadFileDao dao;
    @Resource
    private MinioServiceImpl minioService;

    /**
     * 分页查询
     */
    @Override
    public JSONObject list4Page(JSONObject jsonObject){
        Page<UploadFileEntity> page = CommonUtil.getPage(jsonObject);
        dao.list4Page(page, jsonObject);
        return CommonUtil.successPage(page);
    }

    /**
     * 条件查询
     */
    @Override
    public JSONObject listByCondition(JSONObject jsonObject) {
        return CommonUtil.successJson(dao.listByCondition(jsonObject));
    }
    /**
     * 逻辑删除多条记录
     */
    @Override
    @Transactional
    public JSONObject deleteLogic(List<String> list, String userId) {
        this.update(new LambdaUpdateWrapper<>(UploadFileEntity.class)
                .in(UploadFileEntity::getFileId, list));
        return CommonUtil.successJson();
    }

    /**
     * 根据id查询详情
     */
    @Override
    public JSONObject queryOneById(String id) {
        return CommonUtil.successJson(dao.queryOneById(id));
    }


    /**
     * 保存或修改
     */
    @Override
    @Transactional
    public JSONObject insertOrEdit(UploadFileEntity entity) {
        return CommonUtil.successJson(this.saveOrUpdate(entity));
    }

    /**
     * 上传
     * @Author lililiang
     * @Date 2021/8/27 10:34
     */
    @Override
    public JSONObject upload(String path, MultipartFile file) {
        String urlPath = minioService.upload(path, file);
        UploadFileEntity uploadFileEntity = new UploadFileEntity();
        uploadFileEntity.setFileName(file.getOriginalFilename());
        uploadFileEntity.setFilePath(urlPath);
        this.save(uploadFileEntity);
        return CommonUtil.successJson(uploadFileEntity);
    }

    /**
     * 上传
     * @Author lililiang
     * @Date 2021/8/27 10:34
     */
    @Override
    public JSONObject upload(MultipartFile file) {
        return this.upload(null, file);
    }
}
